package com.sw.dubbo.web.filter;

import com.sw.study.web.util.RequestContext;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * 实现web容器过滤器
 */
public class HttpFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //放入ip
        RequestContext.getContext().setObject("ip",servletRequest.getRemoteAddr());
        filterChain.doFilter(servletRequest,servletResponse);

    }

    @Override
    public void destroy() {
        //销毁
        RequestContext.getContext().destory();
    }
}
